package info.fastpace.utils.entity;

import info.fastpace.utils.Decorator;

import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public interface Entity extends Serializable {
	String getName();
	Feature getFeature(Class<? extends Feature> featureClass);
//	Feature getFeature(Class<? extends Feature> featureClass);

	static class Util {
		private Util() {}
		public static class EntityDecorator<E extends Entity> extends Decorator<E> implements Entity {
			private static final long serialVersionUID = -1705490876192587919L;
			public EntityDecorator(E t) {
				super(t);
			}
			@Override
			public String getName() {
				return getInner().getName();
			}
			@Override
			public Feature getFeature(Class<? extends Feature> featureClass) {
				return null;
			}
		}
		public static class EntityDefault implements Entity {
			private static final long serialVersionUID = -4539902114170498890L;

			private final String name;

			public EntityDefault(String name) {
				super();
				this.name = name;
			}
			@Override
			public String getName() {
				return name;
			}
			@Override
			public Feature getFeature(Class<? extends Feature> featureClass) {
				return getFeature(featureClass, this);
			}

			public static Feature getFeature(Class<? extends Feature> featureClass, Entity entity) {
				if (featureClass.isInstance(entity)) {
					return (Feature) entity;
				}
				return null;
			}
		}
		public static void sortByName(List<? extends Entity> entities) {
			Collections.sort(entities, new EntityNameComparator<Entity>());
		}
		public static class EntityNameComparator<E extends Entity> implements Comparator<E> {
			@Override
			public int compare(Entity lhs, Entity rhs) {
				return lhs.getName().compareToIgnoreCase(rhs.getName());
			}
		}
	}
}
